package com.ruoyi.ade.service.impl;

import java.util.List;

import com.ruoyi.ade.llm.service.IAdeKnowledgeEmbedService;
import com.ruoyi.ade.mapper.AdeKnowledgeRecordMapper;
import com.ruoyi.ade.service.IAdeKnowledgeIndexService;
import com.ruoyi.common.utils.DateUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ade.mapper.AdeKnowledgeBaseMapper;
import com.ruoyi.ade.domain.AdeKnowledgeBase;
import com.ruoyi.ade.service.IAdeKnowledgeBaseService;
import org.springframework.transaction.annotation.Transactional;

/**
 * ADE Knowledge BaseService业务层处理
 *
 * @author wxh
 * @date 2025-07-07
 */
@Service
public class AdeKnowledgeBaseServiceImpl implements IAdeKnowledgeBaseService {
    @Autowired
    private AdeKnowledgeBaseMapper adeKnowledgeBaseMapper;

    @Autowired
    private AdeKnowledgeRecordMapper adeKnowledgeRecordMapper;

    @Autowired
    private IAdeKnowledgeEmbedService adeKnowledgeEmbedService;

    /**
     * 查询ADE Knowledge Base
     *
     * @param id ADE Knowledge Base主键
     * @return ADE Knowledge Base
     */
    @Override
    public AdeKnowledgeBase selectAdeKnowledgeBaseById(Long id) {
        return adeKnowledgeBaseMapper.selectAdeKnowledgeBaseById(id);
    }

    /**
     * 查询ADE Knowledge Base列表
     *
     * @param adeKnowledgeBase ADE Knowledge Base
     * @return ADE Knowledge Base
     */
    @Override
    public List<AdeKnowledgeBase> selectAdeKnowledgeBaseList(AdeKnowledgeBase adeKnowledgeBase) {
        return adeKnowledgeBaseMapper.selectAdeKnowledgeBaseList(adeKnowledgeBase);
    }

    /**
     * 新增ADE Knowledge Base
     *
     * @param adeKnowledgeBase ADE Knowledge Base
     * @return 结果
     */
    @Override
    public int insertAdeKnowledgeBase(AdeKnowledgeBase adeKnowledgeBase) {
        adeKnowledgeBase.setCreateTime(DateUtils.getNowDate());
        return adeKnowledgeBaseMapper.insertAdeKnowledgeBase(adeKnowledgeBase);
    }

    /**
     * 修改ADE Knowledge Base
     *
     * @param adeKnowledgeBase ADE Knowledge Base
     * @return 结果
     */
    @Override
    public int updateAdeKnowledgeBase(AdeKnowledgeBase adeKnowledgeBase) {
        return adeKnowledgeBaseMapper.updateAdeKnowledgeBase(adeKnowledgeBase);
    }

    /**
     * 批量删除ADE Knowledge Base
     *
     * @param ids 需要删除的ADE Knowledge Base主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAdeKnowledgeBaseByIds(Long[] ids) {
        boolean drop = false;
        for (Long id : ids) {
            try {
                //先删除milvus索引
                AdeKnowledgeBase adeKnowledgeBase = adeKnowledgeBaseMapper.selectAdeKnowledgeBaseById(id);
                drop = adeKnowledgeEmbedService.dropCollection(adeKnowledgeBase.getCollectionName());
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
            if (drop)
                adeKnowledgeRecordMapper.deleteAdeKnowledgeRecordByBaseId(id);
        }
        if (drop)
            return adeKnowledgeBaseMapper.deleteAdeKnowledgeBaseByIds(ids);
        return 0;
    }

    /**
     * 删除ADE Knowledge Base信息
     *
     * @param id ADE Knowledge Base主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAdeKnowledgeBaseById(Long id) {
        boolean drop = false;
        try {
            //先删除milvus索引
            AdeKnowledgeBase adeKnowledgeBase = adeKnowledgeBaseMapper.selectAdeKnowledgeBaseById(id);
            drop = adeKnowledgeEmbedService.dropCollection(adeKnowledgeBase.getCollectionName());
        } catch (Exception e) {  //删除milvus索引失败
            e.printStackTrace();
            return 0;
        }
        if (drop) {
            //删除知识库中所有记录
            adeKnowledgeRecordMapper.deleteAdeKnowledgeRecordById(id);
            //删除知识库
            return adeKnowledgeBaseMapper.deleteAdeKnowledgeBaseById(id);
        } else {
            return 0;
        }
    }
}
